package com.hjq.fcloud.ui.activity

import android.os.Environment
import android.view.View
import com.hjq.fcloud.R
import com.hjq.fcloud.app.AppActivity
import com.hjq.fcloud.http.api.ProjectImageUploadDetailApi
import com.hjq.fcloud.http.api.ProjectImageUploadDetailBean
import com.hjq.fcloud.http.model.HttpData
import com.hjq.fcloud.jump.CommonJumpBean
import com.hjq.fcloud.utils.AppConfigUtils
import com.hjq.fcloud.utils.FileUtils
import com.hjq.fcloud.utils.Logger
import com.hjq.http.EasyHttp
import com.hjq.http.body.JsonBody
import com.hjq.http.listener.HttpCallback
import com.obs.services.ObsClient
import com.obs.services.exception.ObsException
import java.io.File


class TestActivity : AppActivity() {

    //选中的需求
    val selectProjectBean by lazy { AppConfigUtils.getSelectProjectItemBean() }

    override fun getLayoutId(): Int {
        return R.layout.test_activity
    }

    override fun initView() {
    }

    override fun initData() {}

    /**
     * 判断形象进度是新增还是添加
     */
    private fun checkProjectImageUploadAddOrDetail(
        type: Int,
        callBack: (isAdd: Boolean) -> Unit
    ) {
        var parms = HashMap<String, Any?>()
        parms.put("fnc_id", selectProjectBean?.selectFncItemBean?.id)
        parms.put("type", type)

        EasyHttp.post(this)
            .api(ProjectImageUploadDetailApi())
            .body(JsonBody(parms))
            .request(object : HttpCallback<HttpData<ProjectImageUploadDetailBean>?>(this) {
                override fun onSucceed(result: HttpData<ProjectImageUploadDetailBean>?) {
                    callBack(false)
                }

                override fun onFail(e: java.lang.Exception?) {
                    callBack(true)
                }
            })
    }

    fun jumpProjectImageUploadAddEndActivity(view: View) {
        checkProjectImageUploadAddOrDetail(ProjectImageUploadAddActivity.TYPE_PROGRESS_END) { isAdd ->
            var bean = CommonJumpBean()
            if (isAdd) {
                bean.bizId = null
            }
            ProjectImageUploadAddEndActivity.start(context, bean)
        }
    }

    fun jumpProjectImageUploadAddStartActivity(view: View) {
        checkProjectImageUploadAddOrDetail(ProjectImageUploadAddActivity.TYPE_PROGRESS_START) { isAdd ->
            var bean = CommonJumpBean()
            if (isAdd) {
                bean.bizId = null
            }
            ProjectImageUploadAddStartActivity.start(context, bean)
        }
    }

    fun jumpCognovitActionemActivity(view: View) {
        var bean = CommonJumpBean()
        bean.bizId = "222"
        bean.isStartSign = true
        bean.setBizCode("450")
        CognovitActionemActivity.start(context, bean)
    }


    fun jumpRegulatoryMaterialConfirmationActivity(view: View) {
        var bean = CommonJumpBean()
        bean.bizId = "199"
        bean.setBizCode("440")
        bean.isStartSign = true
        RegulatoryMaterialConfirmationActivity.start(context, bean)
    }

    fun jumpPlatformServiceFeeSignActivity(view: View) {
        var bean = CommonJumpBean()
        bean.bizId = "222"
        bean.setBizCode("57")
        bean.isStartSign = true
        PlatformServiceFeeSignActivity.start(context, bean)
    }

    fun handlerFinancingOverview(view: View) {
        FinancingOverviewActivity.start(context, "")
    }

    fun handlerSign(view: View) {
        startActivity(SignActivity::class.java)
    }

    fun handlerIntercreditorAgreement(view: View) {
//        IntercreditorAgreementActivity.start(this)
    }

    fun handlerMaterialsWarehousingConfirmation(view: View) {
        var jumpBean = CommonJumpBean()
        jumpBean.bizId = "38"
        jumpBean.setBizCode("100401")
        jumpBean.isStartSign = true
        MaterialsWarehousingConfirmationActivity.start(this, jumpBean)
    }


    fun handlerWarehouseReceiptSign(view: View) {
        var jumpBean = CommonJumpBean()
        jumpBean.bizId = "38"
        jumpBean.setBizCode("100401")
        jumpBean.isStartSign = true
        StoreSignActivity.start(this, jumpBean)
    }

    fun handlerProjectImageUploadAdd(view: View) {
        //ProjectImageUploadAddActivity.start(this)
    }

    fun handlerProjectImageUploadDetail(view: View) {
        //ProjectImageUploadAddActivity.start(this, ProjectImageUploadBean.buildTest())
    }

    fun handlerMeetingSummary(view: View) {
//        MeetingSummaryActivity.start(this)
    }

    fun handlerHorizontalView(view: View) {
        startActivity(HorizontalViewActivity::class.java)
    }

    fun handlerUploadFile(view: View) {
        try {
            val downloadsDir =
                Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)
            var filePath = downloadsDir.path + "/test.jpg"
            Thread { uploadFile(filePath) }.start()
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    private val END_POINT = "obs.cn-southwest-2.myhuaweicloud.com"
    private val BUCKET_NAME_FACE = "dgfcloud-obs"
    private val AK = "RYNO0YKPRHBN0EVECO5V"
    private val SK = "wEeCsexVgaO8KJVH7QkuVfugiKJN2w18qqrawvp3"

    //各个目录，好区分
    private val DIR_FACE = "face"
    private val DIR_SIGN = "sign"
    private val DIR_IMAGE = "image"
    private val DIR_VIDEO = "video"
    private fun uploadFile(
        filePath: String,
        bucketName: String? = BUCKET_NAME_FACE,
        dir: String? = DIR_FACE
    ) {
        // 认证用的ak和sk硬编码到代码中或者明文存储都有很大的安全风险，建议在配置文件或者环境变量中密文存放，使用时解密，确保安全；本示例以ak和sk保存在环境变量中为例，运行本示例前请先在本地环境中设置环境变量ACCESS_KEY_ID和SECRET_ACCESS_KEY_ID。
        // 您可以登录访问管理控制台获取访问密钥AK/SK，获取方式请参见https://support.huaweicloud.com/usermanual-ca/ca_01_0003.html
        val ak = AK
        val sk = SK
        val endPoint = END_POINT
        // 创建ObsClient实例
        val obsClient = ObsClient(ak, sk, endPoint)
        try {
            // localfile为待上传的本地文件路径，需要指定到具体的文件名
            var fileName = FileUtils.getFileName(filePath)
            var ret = obsClient.putObject(
                BUCKET_NAME_FACE,
                "fcloud/web/app/$dir/" + fileName,
                File(filePath)
            )
            Logger.i("uploadFile ret:" + ret.objectUrl)
        } catch (e: ObsException) {
            e.printStackTrace()
            var errorCode = e.errorCode
            var errorMessage = e.errorMessage
            Logger.i("uploadFile fail errorCode:" + errorCode + ",errorMessage:" + errorMessage)
        } finally {
            obsClient.close()
        }
    }

}